layui.define(['jquery', 'layer', 'user', 'setter', 'util', 'request'], function (exports) {

    var $ = layui.jquery
        , $win = $(window)
        , $body = $('body')
        , request = layui.request
        , layer = layui.layer
        , events = layui.setter.events
        , user = layui.user
        , view = {
        screen: function () {  // 屏幕判断
            var width = $win.width()
            if (width >= 1200) {
                return 3; //大屏幕
            } else if (width >= 992) {
                return 2; //中屏幕
            } else if (width >= 768) {
                return 1; //小屏幕
            } else {
                return 0; //超小屏幕
            }
        }
        , openIframe: function (config) {
            layer.open($.extend({
                type: 2, title: '提示',
                area: ['800px', '600px'],
                maxmin: true,
                zIndex: 9999999,
                shade: 0.8,
                content: ''
            }, config))
        }
        , openPage: function (config) {
            layer.open($.extend({
                type: 1,
                title: '提示',
                area: ['800px', '600px'],
                maxmin: true,
                zIndex: 9999999,
                shade: 0.8,
                content: ''
            }, config))
        }
    };

    !function () {

        var cfg = {
            "reset-password": {
                'sendData': function (value, row) {
                    return {id: user.user().id, password: value}
                }
            }
        }

        $body.on('click', '*[yak-event]', function () {
            var event = $(this).attr('yak-event')
            var dataMap = {};
            var data = $(this).context.dataset;
            for (const key in data) {
                dataMap[key] = data[key];
            }
            events[event] && events[event]({config: $.extend(cfg[event] || {}, dataMap)});
        });

    }();

    // 获取远端configs接口数据
    view.remoteConfig = function (url, method = "POST") {
        var res = request.send(url, method, {}, false)
        if (res.code === 200) {
            return res.data;
        } else {
            return {}
        }
    }

    view.toTableLabelText = function (value, labels) {
        if (!labels || !labels[value]) {
            return '';
        } else {
            return labels[value]
        }
    }

    /**
     * 渲染下拉列表
     * @param id
     * @param obj
     */
    view.renderSelect = function (id, obj) {
        if (!obj) {
            return;
        }
        // 动态添加选项
        var adminStatusSearch = document.getElementById(id);
        adminStatusSearch.innerHTML = '<option value="">--请选择--</option>';
        Object.keys(obj).forEach(key => {
            var option = new Option(obj[key], key);
            adminStatusSearch.add(option);
        });
    }

    /**
     *
     * @param labels 数据列表{key: value}
     * @param targetId 父容器
     * @param name 名称
     * @param selected 已选择的项目[]
     */
    view.renderCheckbox = function (labels, targetId, name, selected) {
        var checkboxList = document.getElementById(targetId);
        checkboxList.innerHTML = "";
        Object.keys(labels).forEach(function (tag, index) {
            var newCheckbox = document.createElement("input");
            newCheckbox.type = "checkbox";
            newCheckbox.name = name + "[" + tag + "]";
            newCheckbox.title = labels[tag];
            newCheckbox.value = tag;
            var checked = false;
            selected.forEach(function (v) {
                if (tag == v) {
                    checked = true;
                }
            })
            newCheckbox.checked = checked;
            newCheckbox.setAttribute('lay-skin', "tag")
            newCheckbox.className = "layui-checkbox";
            checkboxList.appendChild(newCheckbox);
        })
    }

    /**
     * 渲染开关组件
     * @param targetId
     * @param name
     * @param checked
     */
    view.renderSwitch = function (targetId, name, checked) {
        var checkboxList = document.getElementById(targetId);
        checkboxList.innerHTML = "";
        var newCheckbox = document.createElement("input");
        newCheckbox.type = "checkbox";
        newCheckbox.name = name;
        newCheckbox.title = "ON|OFF";
        newCheckbox.checked = checked;
        newCheckbox.setAttribute('lay-skin', "switch")
        newCheckbox.className = "layui-checkbox";
        checkboxList.appendChild(newCheckbox);
    }


    /**
     *
     * @param labels
     * @param targetId 父容器
     * @param name 元素名称
     * @param value 已选择的项目
     */
    view.renderRadio = function (labels, targetId, name, value) {
        var radioList = document.getElementById(targetId);
        radioList.innerHTML = "";

        Object.keys(labels).forEach(function (tag, index) {
            var radio = document.createElement("input");
            radio.type = "radio";
            radio.name = name;
            radio.title = labels[tag];
            radio.checked = (value === tag);
            radio.className = "layui-radio";
            radio.value = tag;
            radioList.appendChild(radio);
        })

    }

    view.formatFileSize = function (bytes, decimalPoint = 2) {
        if (bytes === 0) return "0 B";
        const units = ["B", "KB", "MB", "GB", "TB", "PB"];
        const index = Math.floor(Math.log(bytes) / Math.log(1024));
        return (bytes / Math.pow(1024, index)).toFixed(decimalPoint) + " " + units[index];
    }


    /**
     * 渲染上传组件
     * @param id
     * @param url
     * @param data
     * @param done
     */
    view.renderUploader = function (id, url, data = {}, done = null) {
        layui.upload.render({
            elem: id,
            url: url,
            headers: {"Token": user.token()},
            data: data,
            done: done
        });
    }


    view.getCondition = function (condition, data, flag = false) {
        var where = {};
        layui.each(condition, function (k, v) {
            if (v.indexOf(':') === -1) {
                where[k] = v
            } else if (v.indexOf(':') === 0) {
                where[k] = data[v.substr(1)]
            }
        });

        if (flag === true) {
            var c = []
            Object.keys(where).forEach(function (k) {
                c.push({key: k, value: where[k]});
            })
            return c;
        }

        return where
    };


    exports('view', view)

})